package com.library.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.library.dto.pojo.MessageDto;
import com.library.dto.pojo.PageBean;
import com.library.dto.vo.BookVO;
import com.library.dto.vo.ReaderVo;
import com.library.entity.Admin;
import com.library.entity.Book;
import com.library.entity.Booktype;
import com.library.entity.ReaderType;
import com.library.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 至尊伍人组
 * @since 2018-09-30
 */
@Controller
@RequestMapping ( "/admin" )
public class AdminController {

    @Autowired
    IReaderTypeService readerTypeService;
    @Autowired
    IBookService bookService;
    @Autowired
    IBooktypeService booktypeService;
    @Autowired
    IReaderService readerService;
    @Autowired
    IAdminService adminService;

    @GetMapping ( "/setting" )
    public String sysSetting( HttpServletRequest request ) {

        /*获得所有读者类型的集合*/
        List<ReaderType> readerTypes = readerTypeService.selectList(null);

        request.setAttribute("readerTypes", readerTypes);

        return "admin/systemSetting";
    }

    @RequestMapping ( "/bookmanager/{pageNum}" )
    public String bookManager( @PathVariable ( value = "pageNum" ) int pageNum,
                               Book book,
                               HttpServletRequest request
    ) {

        List<Booktype> booktypes = booktypeService.selectList(new EntityWrapper<Booktype>()
                                                                  .orderBy("typeId"));
        request.setAttribute("booktypes", booktypes);

        if ( book == null ) {
            //            System.out.println("当前不是模糊查询！！！");
            request.getSession().setAttribute("selectBook", false);
            //            request.setAttribute("selectBook", false);
        } else {
            //            System.out.println("当前是模糊查询！！！");
            request.getSession().setAttribute("selectBook", true);
            //            request.setAttribute("selectBook", true);
        }

        PageBean<BookVO> pageBean = bookService.getBookVoList(pageNum, book);
        List<BookVO> bookVoList = pageBean.getList();
        request.setAttribute("bookVoList", bookVoList);
        request.setAttribute("bean", pageBean);

        //        System.out.println(pageBean.getTotalPage());

        return "admin/bookManager";
    }

    @RequestMapping ( "/booktypemanager" )
    public String booktypemanager( HttpServletRequest request ) {

        //获得所有的种类
        List<Booktype> booktypes = booktypeService.selectList(null);

        //用于保存各个种类的图书的的数量
        List<Integer> booktypesCount = new ArrayList<>();

        //遍历
        booktypes.forEach(e -> {
            /**/
            int count = bookService.selectCount(new EntityWrapper<Book>()
                                                    .eq("typeId", e.getTypeId()));
            booktypesCount.add(count);
        });

        request.setAttribute("booktypes", booktypes);
        request.setAttribute("booktypesCount", booktypesCount);

        return "admin/bookTypeManager";

    }

    @RequestMapping ( "/readermanager" )
    public String readermanager( HttpServletRequest request ) {

        List<ReaderVo> readerVos = readerService.selectListAsReaderVo(null);

        request.setAttribute("readerVos", readerVos);

        List<ReaderType> readerTypes = readerTypeService.selectList(null);

        request.setAttribute("readerTypes", readerTypes);

        return "admin/readerManager";
    }

    @RequestMapping ( "/adminmanager" )
    public String adminmanager( HttpServletRequest request ) {

        List<Admin> adminlist = adminService.selectList(null);

        request.setAttribute("adminlist", adminlist);

        return "admin/adminManager";
    }

    @RequestMapping ( "/del/{adminId}" )
    public String del( @PathVariable int adminId ) {

        adminService.deleteById(adminId);

        return "redirect:/admin/adminmanager";
    }

    @RequestMapping ( "/sendemail" )
    public String sendEmail( MessageDto messageDto ) {

        //发送电子邮件
        adminService.sendEmail(messageDto);

        return "redirect:/admin//adminmanager";

    }

    @RequestMapping ( "/sendsms" )
    public String sendSMS() {

        //发送手机短信
        //……

        return "redirect:/admin//adminmanager";

    }

}

